<?php
/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/12/20
 * Time: 下午2:34
 */
class User_model extends CI_Model{
    public function __construct(){
        $this->load->database();
        $this->load->helper("common");
    }

    // 新增app_wechat中open_id
    public function add_wechat_open_id($open_id){
        $sql = "select * from app_wechat WHERE open_id = '$open_id' ";
        $query = $this->db->query($sql);
        $info =  $query->row_array();
        if($info){
            $insert_id = $info['id'];
        }else{
            $data_arr['open_id'] = $open_id;
            $this->db->insert("app_wechat",$data_arr);
            $insert_id = $this->db->insert_id();
        }
        return $insert_id;
    }

    // 存入我的寄件人列表
    public function add_app_user_receiver($user_id,$receiver_user_id){
        $sql = "select * from app_user_receiver WHERE user_id = ".$user_id." and receiver_user_id = ".$receiver_user_id;
        $query = $this->db->query($sql);
        $info =  $query->row_array();
        if($info){
            $insert_id = $info['id'];
        }else{
            $data_arr['user_id'] = $user_id;
            $data_arr['receiver_user_id'] = $receiver_user_id;
            $this->db->insert("app_user_receiver",$data_arr);
            $insert_id = $this->db->insert_id();
        }
        return $insert_id;
    }

    // 用户
    public function user_address_list($requst){
        $desc['page'] = isset($requst['page'])?(int)$requst['page']:1;
        $desc['psize'] = isset($requst['psize'])?(int)$requst['psize']:10;
        $user_id = $requst['uid'];

        $sql = "select * from app_address WHERE user_id =".$user_id;
        // 排序
        $sql.=" order by id desc";
        // 分页
        $limit_from = ($desc['page']-1)*$desc['psize'];
        $sql.=" limit ".$limit_from.",".$desc['psize'];

        $query = $this->db->query($sql);
        $info =  $query->result_array();

        if($info && count($info)>0){
            return $info;
        }else{
            return false;
        }
    }

    // 根据用户ID获取用户和微信信息
    public function query_wechat_info_by_id($id){
        $sql = "select au.status,au.wechat_id,au.deposit,au.created_at,aw.nick_name,aw.avatar_url,aw.open_id,aw.last_visit_time from app_user as au LEFT JOIN app_wechat as aw on au.wechat_id = aw.id WHERE aw.app_type = 1 AND au.id = ".$id;
        $query = $this->db->query($sql);
        return  $query->row_array();
    }

    // 根据用户ID获取用户的地址信息
    public  function query_user_address_info_by_uid($id){
        $sql = "select a.id AS address_id, a.name,a.tel,a.address from app_address AS a WHERE user_id = ".$id ." ORDER BY id DESC";
        $query = $this->db->query($sql);
        return  $query->result_array();
    }


    // 根据用户微信ID确认用户信息
    public function query_wechat_info_by_wechat_id($wechat_id){
        $sql = "select au.id,au.wechat_id,au.deposit,au.created_at,aw.nick_name,aw.avatar_url,aw.open_id,aw.last_visit_time from app_user as au LEFT JOIN app_wechat as aw on au.wechat_id = aw.id WHERE aw.app_type = 1 AND au.wechat_id = ".$wechat_id;
        $query = $this->db->query($sql);
        return  $query->row_array();
    }

    // 根据用户openid确认用户信息
    public function query_wechat_info_by_open_id($open_id){
        $sql = "select au.id,au.wechat_id,au.deposit,au.created_at,aw.nick_name,aw.avatar_url,aw.open_id,aw.last_visit_time from app_user as au LEFT JOIN app_wechat as aw on au.wechat_id = aw.id WHERE aw.app_type = 1 AND aw.open_id = '$open_id' ";
        $query = $this->db->query($sql);
        return  $query->row_array();
    }


    // app_user_box 和 app_user_box_get 关联信息
    public function query_app_user_box($user_id,$status_arr,$page=1,$psize=10,$courier_id=''){
        $sql = "select a.*,b.name,b.tel,b.address from app_user_box a LEFT JOIN app_address b on a.address_id = b.id where 1=1 ";
        if($user_id){
            $sql .= " and a.user_id = $user_id";
        }
        if($status_arr)
        {
            $sq = implode(',',$status_arr);
            $sql .= " and a.status in ($sq)";
        }
        if($courier_id)
        {
            $sql .= " and a.courier_id = $courier_id";
        }

        // 分页
        $limit_from = ($page-1)*$psize;
        $sql.=" limit ".$limit_from.",".$psize;

        $query = $this->db->query($sql);
        $info =  $query->result_array();
        // 查询box_get表相关箱子的信息
        foreach ($info as &$in)
        {
            // LEFT JOIN app_user_box_get b ON a.id = b.user_box_id
            $user_box_id = $in['id'];
            $sql = "select a.box_type_id, a.num, b.name from app_user_box_get a LEFT JOIN app_box_type b ON a.box_type_id = b.id WHERE user_box_id = $user_box_id";
            $query = $this->db->query($sql);
            $res =  $query->result_array();
            if($res){
                $in['box_info'] = $res;
            }else{
                $in['box_info'] = '';
            }
        }

        return $info;
    }

    // 新建召唤快递员信息
    public function save_user_box_info($user_id,$type,$status=1,$address_id,$box_list)
    {
        if($type == 1){
            // 需要箱子
            $this->db->trans_begin();
            $data_arr['user_id'] = $user_id;
            $data_arr['type'] = 1;
            $data_arr['status'] = $status;
            $data_arr['created_at'] = date("Y-m-d H:i:s");
            $data_arr['address_id'] = $address_id;
            $this->db->insert("app_user_box",$data_arr);
            $insert_id = $this->db->insert_id();
            $data_arr2['user_box_id'] = $insert_id;
            $insert_id2 = 1;
            if($box_list)
            {
                foreach ($box_list as $b)
                {
                    $data_arr2['box_type_id'] = $b['box_type_id'];
                    $data_arr2['num'] = $b['num'];
                    $insert_id2 = $this->db->insert("app_user_box_get",$data_arr2);
                    if(!$insert_id2){
                        break;
                    }
                }
            }
            if($insert_id && $insert_id2){
                $this->db->trans_commit();
                return $insert_id;
            }
            $this->db->trans_rollback();
            return false;
        }else{
            // 归还箱子
            $this->db->trans_begin();
            $data_arr['user_id'] = $user_id;
            $data_arr['type'] = 2;
            $data_arr['status'] = $status;
            $data_arr['created_at'] = date("Y-m-d H:i:s");
            $data_arr['address_id'] = $address_id;
            $this->db->insert("app_user_box",$data_arr);
            $insert_id = $this->db->insert_id();
            if($insert_id){
                $this->db->trans_commit();
                return $insert_id;
            }
            $this->db->trans_rollback();
            return false;
        }
    }


}